/*
 * @Author: dgflash
 * @Date: 2022-05-11 13:53:35
 * @LastEditors: dgflash
 * @LastEditTime: 2022-07-06 18:30:51
 */
import chalk from "chalk";
import path from "path";
import { ecs } from "../../../core/ecs/ECS";
import { CommonFactory } from "../../../module/common/CommonFactory";
import { Config, randomRoomServerUrl } from "../../../module/config/Config";
import { HallServer } from "../HallServer";



/** 启动匹配服务器 */
@ecs.register('HallServerStart')
export class HallServerStartComp extends ecs.Comp {
    reset(): void { }
}

export class HallServerStartSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(HallServerStartComp);
    }

    async entityEnter(e: HallServer) {
        let server = CommonFactory.createHsHall();
        e.HallModel.hsHall = server

        const roomServerUrl = randomRoomServerUrl();
        console.error("roomServerUrl:", roomServerUrl);
        e.HallModel.hsRoom = CommonFactory.createWscRoom(roomServerUrl, server);
        console.error("e.HallModel.hsRoom:", e.HallModel.hsRoom);

        // 如果指定 autoImplementApi 的第 2 个参数为 true，则开启延迟挂载，即延迟到对应接口被调用时才执行挂载操作，加快冷启动速度
        await server.autoImplementApi(path.resolve(__dirname, '../api'), true);
        server.logger.log(chalk.green(`[大厅服务器] 服务已初始化完成`));
        
        // 启动匹配服务器
        await server.start();
        server.logger.log(chalk.green(`[大厅服务器] 启动成功`));

        // 定时 log 播报房间状态
        //var rooms = e.HallModel.rooms;
        /*setInterval(() => {
            server.logger.log(`
        [匹配服务器状态播报]
        - 房间已连接数量 = ${rooms.count(v => !!v.state)}
        - 房间连接中数量 = ${rooms.count(v => !v.state)}
        - 房间总数　　　 = ${rooms.sum(v => v.state?.rooms.length ?? 0)}
        - 房内用户数　　 = ${rooms.sum(v => v.state?.rooms.sum(v => v.amount) ?? 0)}`);
        }, Config.match.interval_logger);*/

        // 定时匹配验证（考虑修改为触发时匹配验证）
        //e.add(MatchStartComp);
    }
}